package com.fsh.anime.controller;

import com.fsh.anime.model.dtos.ReplyCommentDto;
import com.fsh.anime.model.dtos.SaveCommentDto;
import com.fsh.anime.model.vos.Result;
import com.fsh.anime.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/comment")
@Slf4j
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 新增评论
     */
    @PostMapping("/saveComment")
    public Result saveComment(@RequestBody @Validated SaveCommentDto dto, BindingResult results){
        if(results.hasErrors()){
            return Result.error(results.getFieldError().getDefaultMessage());
        }
        return commentService.saveComment(dto);
    }

    /**
     * 获取评论
     */
    @GetMapping("/getComment")
    public Result getComment(String articleId){
        return commentService.getComment(articleId);
    }

    /**
     * 回复评论
     */
    @PostMapping("/replyComment")
    public Result replyComment(@RequestBody @Validated ReplyCommentDto dto,BindingResult results){
        if(results.hasErrors()){
            return Result.error(results.getFieldError().getDefaultMessage());
        }
        return commentService.replyComment(dto);
    }

}
